<template>
  <div class="register-container">
    <div class="register-card">
      <h3 class="title">计考通系统登录页面</h3>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="register-form"
        @submit.prevent="handleLogin">
        <div class="form-group">
          <label class="label-text" for="username">用户名</label>
          <el-form-item prop="username">
            <input type="text" id="username" v-model="loginForm.username" placeholder="请输入用户名" class="input-field" />
          </el-form-item>
        </div>
        <div class="form-group">
          <label class="label-text" for="password">密码</label>
          <el-form-item prop="password">
            <input type="password" id="password" v-model="loginForm.password" placeholder="请输入密码" class="input-field" />
          </el-form-item>
        </div>
        <!-- 添加忘记密码链接 -->
        <div class="forgot-password" @click="handleForgotPassword">忘记密码</div>
        <div class="form-actions">
          <button type="submit" class="register-btn" :disabled="loading">
            {{ loading ? '登录中...' : '登录' }}
          </button>
          <button type="button" class="register-btn" @click="handleRegister">
            注册
          </button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../store/UserStore'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()
const loginFormRef = ref(null)
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    const result = await userStore.login(loginForm.username, loginForm.password)
    
    if (result.success) {
      // 确保登录数据已写入 localStorage
      localStorage.setItem('user', JSON.stringify({
        isLoggedIn: true,
        role: userStore.role,
        token: userStore.token,
        userInfo: userStore.userInfo,
        avatarUrl: userStore.avatarUrl,

      }))
      
      ElMessage.success('登录成功')
      if (userStore.getRole === 'admin') {
        router.push('/admin')
      } else {
        router.push('/home')
      }
    } else {
      ElMessage.error(result.error || '登录失败')
    }
  } catch (error) {
    console.error('登录错误:', error)
    ElMessage.error(error.response?.data?.message || '登录失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

// 处理忘记密码点击事件
const handleForgotPassword = () => {
  router.push('/forget-password')
}

// 处理注册点击事件
const handleRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
/* 大容器样式 */
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(120deg, #E0F7FA 0%, #B2EBF2 100%);
}

/* 注册卡片样式 */
.register-card {
  width: 340px;
  padding: 40px;
  border-radius: 24px;
  background: linear-gradient(135deg, #4FC3F7 0%, #81D4FA 100%); 
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1); 
  color: #fff;
  text-align: center;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  position: relative;
}

.register-card::before {
  content: "🔑";
  position: absolute;
  top: -20px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 30px;
}

.register-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.15); 
}

/* 标题样式 */
.title {
  margin-bottom: 32px;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 2px;
  text-transform: uppercase;
  animation: titleSlide 1s ease-in-out;
}

@keyframes titleSlide {
  from {
    transform: translateY(-20px);
    opacity: 0;
  }

  to {
    transform: translateY(0);
    opacity: 1;
  }
}

/* 表单组通用样式 */
.form-group {
  margin-bottom: 24px;
  text-align: left;
  animation: formFade 1s ease-in-out;
}

@keyframes formFade {
  from {
    opacity: 0;
    transform: translateY(10px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 标签文本样式 */
.label-text {
  display: block;
  margin-bottom: 8px;
  font-size: 15px;
  color: #fff;
  font-weight: 500;
}

/* 输入框样式 */
.input-field {
  width: 100%;
  height: 44px;
  padding: 0 14px;
  border: none;
  border-radius: 16px;
  background-color: #E0F7FA; 
  color: #333;
  font-size: 15px;
  transition: box-shadow 0.3s ease, transform 0.3s ease;
}

.input-field:focus {
  outline: none;
  box-shadow: 0 0 12px rgba(79, 195, 247, 0.6); 
  transform: scale(1.02); 
}

/* 忘记密码链接样式 */
.forgot-password {
  margin-bottom: 10px;
  font-size: 15px;
  color: #fff;
  text-decoration: underline;
  cursor: pointer;
  transition: color 0.3s ease, transform 0.3s ease;
  animation: formFade 1s ease-in-out;
  animation-delay: 0.2s;
  opacity: 0;
  animation-fill-mode: forwards;
}

.forgot-password:hover {
  color: #03A9F4;
  transform: translateY(-1px);
}

/* 修改表单操作区域样式 */
.form-actions {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  /* 添加按钮之间的间距 */
  margin-top: 24px;
}

/* 登录按钮样式 */
.register-btn {
  flex: 1;
  /* 让按钮平均分配空间 */
  width: auto;
  /* 移除固定宽度 */
  height: 48px;
  border: none;
  border-radius: 16px;
  background: linear-gradient(90deg, #03A9F4 0%, #29B6F6 100%); 
  color: #fff;
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  animation: formFade 1s ease-in-out;
  animation-delay: 0.4s;
  opacity: 0;
  animation-fill-mode: forwards;
  position: relative;
}

.register-btn::after {
  content: "👋";
  position: absolute;
  right: 15px;
  opacity: 0;
  transition: all 0.3s ease;
}

.register-btn:hover {
  box-shadow: 0 4px 16px rgba(3, 169, 244, 0.4); 
  transform: translateY(-2px);
}

.register-btn:hover::after {
  opacity: 1;
  right: 20px;
}

.register-btn:active {
  transform: scale(0.96);
}

.register-btn:disabled {
  background: #bdc3c7;
  color: #fff;
  cursor: not-allowed;
  box-shadow: none;
  transform: none;
}

/* 隐藏 Element Plus 表单验证的默认样式 */
:deep(.el-form-item) {
  margin-bottom: 0;
}

:deep(.el-form-item__content) {
  display: block;
}

:deep(.el-form-item__error) {
  color: #ff6b6b;
  font-size: 12px;
  margin-top: 5px;
}
</style>